// 2014-03-10 Tyrghen
//---------------------------------------------
// Usage:
//  ["other_marker"] call edt_fnc_marker;
//  ["marker_name",[12345,34567,0],"ICON","ColorBlufor",[0.6,0.6],"STR_marker_text",90,"hd_flag","",1] call edt_fnc_marker;
//  [[12345,34567,0],"ELLIPSE","ColorBlufor",[150,250],"",90,"","Horizontal",1] call edt_fnc_marker;
//---------------------------------------------
// Purpose:
// Create a JIP Synchronized marker on the map
// 
// Parameters:
// Option 1 - Copy from another marker
// - Name of the marker to copy
//   Now you'll ask, what's the point if I already see it?
//   Well, take the case when you want to show markers for enemy troops during the mission. You will place a marker in the editor, change it to your liking,
//   name it like "marker2_0" so it is not visible at mission start. Then, during the course of the mission, just do:
//   _group_marker = ["marker2_0"] call edt_fnc_marker;
//   _group_marker setMarkerPos (getPos leader _group);
//
// Option 2 - Create a marker from scratches with a specific name
// - name of the marker to createAgent
// - marker position
// - marker shape (ICON / ELLIPSE / RECTANGLE)
// - marker color
// - marker size
// - marker text: can be a magic string for local translation
// - marker orientation
// - marker type if "ICON" was selected as shape
// - marker brush if "ELLIPSE" or "RECTANGLE" has been chosen as shape
// - marker alpha transparency
// In this case, while you can build the marker from scratch, a typical use would be to create an empty marker with a specific name.
// For example, to place a new box ("box_2", "box_3",...), to place a new respawn point ("respawn_1","respawn_2",etc.)
//
// Option 3 - Create a marker from scratches but with a random name
// - marker position
// - marker shape (ICON / ELLIPSE / RECTANGLE)
// - marker color
// - marker size
// - marker text: can be a magic string for local translation
// - marker orientation
// - marker type if "ICON" was selected as shape
// - marker brush if "ELLIPSE" or "RECTANGLE" has been chosen as shape
// - marker alpha transparency
// Simply to add a marker on the map at runtime in one call
//
// Returns:
// Marker Name
//---------------------------------------------
// Objectif:
// 
//
// Paramètres:
// - 
//
// Renvoie:
// 
//---------------------------------------------

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "editor\fn_marker.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

if (!isServer) exitWith {TRACE("Not Server")};

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_m_name","_m_pos","_m_shape","_m_col","_m_size","_m_text","_m_dir","_m_type","_m_brush","_m_alpha","_inc"];
_inc = 0;
_m_name = "";
if (typename (_this select 0) == typename "" && {(count _this > 1)} && {((typename (_this select 1) == typename "") || (typename (_this select 1) == typename []))}) then {
	_inc = 1;
	_m_name = _this select 0;
};
_m_shape = "ICON";
_m_col = "Default";
_m_size = [];
_m_text = "";
_m_dir = 0;
_m_type = "Empty";
_m_brush = "Solid";
_m_alpha = -888888;

DEFAULT_PARAM(_m_pos,(_inc),ARR_3(0,0,0));
if (IS_OBJECT(_m_pos)) then {
	_m_pos = getPos _m_pos;
};
if (IS_STRING(_m_pos)) then {
	if (IS_MARKER(_m_pos)) then {
		_m_shape = markerShape _m_pos;
		_m_col = markerColor _m_pos;
		_m_size = markerSize _m_pos;
		_m_text = markerText _m_pos;
		_m_dir = markerDir _m_pos;
		_m_type = markerType _m_pos;
		_m_brush = markerBrush _m_pos;
		_m_alpha = markerAlpha _m_pos;
		_m_pos = markerPos _m_pos;
	};
};	

DEFAULT_PARAM(_m_shape,(_inc + 1),_m_shape);
DEFAULT_PARAM(_m_col,(_inc + 2),_m_col);
DEFAULT_PARAM(_m_size,(_inc + 3),_m_size);
DEFAULT_PARAM(_m_text,(_inc + 4),_m_text);
DEFAULT_PARAM(_m_dir,(_inc + 5),_m_dir);
DEFAULT_PARAM(_m_type,(_inc + 6),_m_type);
DEFAULT_PARAM(_m_brush,(_inc + 7),_m_brush);
DEFAULT_PARAM(_m_alpha,(_inc + 8),_m_alpha);

if (_m_name == "") then {
	if (isnil QGVAR(MARKER_INDEX)) then { GVAR(MARKER_INDEX) = 0; };
	INC(GVAR(MARKER_INDEX));
	_m_name = format[QGVAR(MARKER_%1),GVAR(MARKER_INDEX)];
};


if (!IS_ARRAY(_m_pos)) exitWith {ERROR("Position is not an array")};
if (_m_shape == "" ) then { _m_shape = "ELLIPSE" };
if (_m_col == "" ) then { _m_col = "Default" };
if (count _m_size <= 0) then { _m_size = [1,1] };
if (_m_type == "" ) then { _m_type = "Empty" };
if (_m_brush == "" ) then { _m_brush = "Solid" };

_params = [_m_name,_m_pos,_m_shape,_m_col,_m_size,_m_text,_m_dir,_m_type,_m_brush,_m_alpha];
TRACE_1("Sending Params",_params);

if (!(isNil QGVAR(INITIALIZED)) && {GVAR(INITIALIZED)}) then {
	[QGVAR(all_marker),[_params,TMF_SERVER_INSTANCE_ID,false],THIS_FILE] call FFUNC(callEvent);
} else {
	_params spawn {
		waituntil {(!(isNil QGVAR(INITIALIZED)) && {GVAR(INITIALIZED)})};
		DEBUG_1("Delayed marker init",_this);
		sleep 0.5;
		[QGVAR(all_marker),[_this,TMF_SERVER_INSTANCE_ID,false],THIS_FILE] call FFUNC(callEvent);
	};
};


TRACE("End");
TMFPROFILERSTOP;
_m_name;